package com.mbyte.easy.admin.controller;


import com.mbyte.easy.admin.entity.Relation;
import com.mbyte.easy.admin.service.IRelationService;
import com.mbyte.easy.common.constant.SysRole;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassNameGroupingController
 * @Description 人员分组
 * @Author myr
 * @Date 2019/12/9 9:27
 * @Version 1.0
 **/
@Controller
@RequestMapping("/group/group")
public class GroupingController extends BaseController {

    private String prefix = "admin/relation/group/";

    @Autowired
    private IRelationService relationService;
    @Autowired
    private IUserService userService;

    /**
     * 版块负责人列表
     * @param model
     * @param pageNo
     * @param pageSize
     * @param name 按名字查询板块负责人
     * @Author myr
     * @return
     */
    @RequestMapping
    public String index(Model model,
                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        SysUser user = new SysUser();
        if (name != null && !"".equals(name.trim())) {
            user.setUsername(name);
            model.addAttribute("name", name.trim());
        }
        model.addAttribute("pageInfo", new PageInfo<SysUser>(relationService.index(pageNo,pageSize,name)));
        return this.prefix + "group-list";
    }

    /**
     * 分配写手前跳转页面
     * @param id 版块负责人id
     * @Author myr
     * @return
     */
    @GetMapping("addBefore/{id}")
    public String addBefore(Model model,@PathVariable("id")Long id){
        List<SysUser> userList = relationService.addBefore();
        model.addAttribute("roleId", (long) SysRole.Writer);
        model.addAttribute("writerList",userList);
        model.addAttribute("leader",userService.selectByPrimaryKey(id));
        return prefix+"add";
    }

    /**
     * 为板块负责人分配写手
     * 先查出所有未分配的写手的id（先查出写手id 不在未分配表里然后就OK）
     * 将所有写手列出来，让他勾选，完成分配
     * @param relation
     * @Author myr
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(String[] users, Relation relation){
        return toAjax(relationService.add(users,relation));
    }

    /**
     * 稿件校对组长列表
     * @param model
     * @param pageNo
     * @param pageSize
     * @param name 按名字查询校对组长
     * @Author myr
     * @return
     */
    @RequestMapping("draftCheckLeaderIndex")
    public String draftCheckLeaderIndex(Model model,
                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        SysUser user = new SysUser();
        if (name != null && !"".equals(name.trim())) {
            user.setUsername(name);
            model.addAttribute("name", name.trim());
        }
        model.addAttribute("pageInfo", new PageInfo<SysUser>(relationService.draftCheckLeaderIndex(pageNo,pageSize,name)));
        return this.prefix + "group-leaderList";
    }

    /**
     * 分配稿件校对人前跳转页面
     * @param id 稿件校对组长id
     * @Author myr
     * @return
     */
    @GetMapping("addCheckerBefore/{id}")
    public String addCheckerBefore(Model model,@PathVariable("id")Long id){
        List<SysUser> userList = relationService.addCheckerBefore();
        model.addAttribute("roleId", (long) SysRole.Draft_Checker);
        model.addAttribute("checkerList",userList);
        model.addAttribute("leader",userService.selectByPrimaryKey(id));
        return prefix+"addChecker";
    }

    /**
     * 为稿件校对组长分配稿件校对人
     * 先查出所有未分配的写手的id（先查出写手id 不在未分配表里然后就OK）
     * 将所有写手列出来，让他勾选，完成分配
     * @param relation
     * @Author myr
     * @return
     */
    @PostMapping("addChecker")
    @ResponseBody
    public AjaxResult addChecker(String[] users, Relation relation){
        return toAjax(relationService.addChecker(users,relation));
    }
}
